from . import web
from flask import flash,render_template,redirect, url_for, request
from flask_login import login_required, current_user
from app.formsjiaoyan.book import DriftForm
from app.libs.email import send_mail
from app.libs.enums import PendingStatus
from app.models.base import db
from app.models.drift import Drift
from app.models.gift import Gift
from app.models.user import User
from app.models.wish import Wish
from app.view_models.book import BookViewModel
from sqlalchemy import or_,desc
from app.view_models.drift import DriftCollection

__author__ = '七月'


@web.route('/drift/<int:gid>', methods=['GET', 'POST'])
@login_required
def send_drift(gid):
    current_gift = Gift.query.get_or_404(gid)
    if current_gift.is_yourself_gift(current_user.id):
        flash("这本书是你自己的，不能自己向自己索要书籍喔")
        return redirect(url_for("web.book_detail",isbn=current_gift.isbn))

    can = current_user.can_send_drift()
    if not can:
        return render_template("not_enough_beans.html",beans=current_user.beans)
     #页面渲染展示的信息，这个有具体意义所以没必要写在viewmodel里，然后索要者鱼豆可以从current_user中获取
    gifter = current_gift.user.Summary
    form = DriftForm(request.form)
    if request.method == 'POST' and form.validate():
        save_drift(form,current_gift)
        #send_mail
        send_mail(current_gift.user.email,"有人想要一本书",
                  'email/get_gift.html',wisher=current_user,gift=current_gift)
        return redirect(url_for("web.pending"))


    return render_template("drift.html",
                           gifter = gifter,
                           user_beans = current_user.beans,form = form)



@web.route('/pending')
@login_required
def pending():
    drifts = Drift.query.filter(
        or_(Drift.requester_id==current_user.id,
            Drift.gifter_id==current_user.id)).order_by(desc(Drift.create_time)).all()

    views = DriftCollection(drifts,current_user.id)
    return render_template('pending.html',drifts=views.data)
@web.route('/drift/<int:did>/reject')
@login_required
#filter查询两张表类.属性查询
def reject_drift(did):
    with db.auto_commit():
        drift = Drift.query.filter(Gift.uid== current_user.id,Drift.id == did).first_or_404()
        drift.pending = PendingStatus.Reject
        requester = User.query.get_or_404(drift.requester_id)
        requester.beans += 1
    return redirect(url_for("web.pending"))


@web.route('/drift/<int:did>/redraw')
@login_required
def redraw_drift(did):
    with db.auto_commit():
        drift = Drift.query.filter_by(id = did,requester_id = current_user.id).first_or_404()
        drift.pending = PendingStatus.Redraw
        current_user.beans +=1
    return redirect(url_for("web.pending"))



@web.route('/drift/<int:did>/mailed')
@login_required
def mailed_drift(did):
    with db.auto_commit():
        drift = Drift.query.filter_by(gifter_id = current_user.id,id = did).first_or_404()
        drift.pending = PendingStatus.Success
        current_user.beans +=1
        gift = Gift.query.filter_by(id = drift.gift_id).first_or_404()
        gift.launched = True
        Wish.query.filter_by(isbn = drift.isbn,
                             uid = drift.request_id,
                             launched = False).update({Wish.launched:True})
        return redirect(url_for("web.pending"))



def save_drift(drift_form,current_gift):
    with db.auto_commit():
        drift = Drift()
        #以下写法比较绕，一个个赋值
        # drift.message =drift_form.message.data
        # drift.mobile =drift_form.mobile.data
        # drift.address = drift_form.address.data
        # drift.recipient_name = drift_form.recipient_name.data
        #以下简单写法,使用populate_obj，但是前提是model层的drift模型字段与book校验层form那里的字段一样
        drift_form.populate_obj(drift)
        drift.gift_id = current_gift.id
        drift.requester_id=current_user.id
        drift.requester_nickname = current_user.nickname
        drift.gifter_nickname = current_gift.user.nickname
        drift.gifter_id = current_gift.user.id
        book = BookViewModel(current_gift.book)
        drift.book_title=book.title
        drift.book_img = book.image
        drift.book_author =book.author
        drift.isbn =book.isbn
        if current_user.beans>=1:
            current_user.beans -= 1
        else:
            flash("你的豆豆不足，确保鱼豆充足喔")
        db.session.add(drift)
        # book = current_gift.book.first
        # drift.book_title = book["title"]
        # drift.book_author=book["author"]
        # drift.book_img = book["image"]
        # drift.isbn = book["isbn"]